(function($) {
  "use strict";
  /*
  * author Jayden
  * params = {
  * }
  */
  $.serviceSelect = function(option) {
    var defaults = {
      mustSelectList: [ // 单选项数据
        {
          serviceName: "签证人数1",
          servicePrice: 200,
          serviceCount: 0
        },{
          serviceName: "签证人数2",
          servicePrice: 200,
          serviceCount: 0
        },{
          serviceName: "签证人数3",
          servicePrice: 200,
          serviceCount: 0
        }
      ],
      monomialSelectList: [ // 固定项数据
        {
          serviceName: "泰国签证港澳台及外籍人士机票附加费泰国签证港澳台 及外籍人士机票附加费",
          servicePrice: 200,
          serviceCount: 0
        },{
          serviceName: "泰国签证港2",
          servicePrice: 200,
          serviceCount: 0
        }
      ],
      perRoomPerDayList: [ // 每房每天数据
        {
          roomNumber: "房间1",
          roomInfo: [{date: "12月20日",price: 200,count: 0},{date: "12月20日",price: 200,count: 0}]
        },{
          roomNumber: "房间2",
          roomInfo: [{date: "12月20日",price: 200,count: 0},{date: "12月20日",price: 200,count: 0}]
        }
      ],
      perRoomList: [
        {
          roomNumber: "房间1",
          roomPrice: 200,
          roomCount: 0
        },{
          roomNumber: "房间2",
          roomPrice: 200,
          roomCount: 0
        },{
          roomNumber: "房间3",
          roomPrice: 200,
          roomCount: 0
        }
      ],
      serviceGroupList: [
        {
          roomNumber: "房间1",
          roomInfo: [{name: "海贼王主题",price: 200},{name: "海贼王主题",price: 200}]
        },{
          roomNumber: "房间2",
          roomInfo: [{name: "海贼王主题",price: 200},{name: "海贼王主题海贼王主题海贼王主题海贼王主题",price: 200}]
        }
      ],
      confirmFn: function() {}
    };

    var params = $.extend({},defaults,option);
    // console.log(params);
    var html = _.template($("#service-select").html())(params);
    var serviceSelect = $(html).appendTo(document.body);
    if (params.mustSelectList.length <= 0) {
      
    }
    $.pushHistory();
    serviceSelect.show().addClass("show");

    // 单选项加减
    $(".must-select-box .gzl-cell .gzl-count span").on("click", function () {
      if (!$(this).hasClass("disabled")) {
        var numlist, selectlist, max, min, num, val, siblingsNum;
        numlist = $(this).parents(".gzl-cell");
        selectlist = $(this).parents(".must-select-box");
        max = selectlist.data("max");
        min = selectlist.data("min");
        num = $(this).parent().find(".gzl-count__num");
        val = parseInt(num.text());
        siblingsNum = numlist.siblings().find(".gzl-count__num");
        if ($(this).hasClass("gzl-count__plus")) { // 加
          if (val < max) {
            $(this).parent().find(".gzl-count__minus").removeClass("disabled");
            siblingsNum.text(0).siblings(".gzl-count__minus").addClass("disabled");
            num.text(val + 1);
            if (val + 1 === max) {
              numlist.find(".gzl-count__plus").addClass("disabled");
            }
          }
        } else if ($(this).hasClass("gzl-count__minus")) { // 减
          if (val > min) {
            numlist.find(".gzl-count__plus").removeClass("disabled");
            num.text(val - 1);
            if (val - 1 === 0) {
              $(this).addClass("disabled");
            }
          }
        }
      }
    })

    // 固定项加减
    $(".monomial-select .gzl-cell .gzl-count span").on("click", function () {
      if (!$(this).hasClass("disabled")) {
        var numlist, max, min, num, val;
        numlist = $(this).parents(".gzl-cell");
        max = numlist.data("max");
        min = numlist.data("min");
        num = $(this).parent().find(".gzl-count__num");
        val = parseInt(num.text());
        if ($(this).hasClass("gzl-count__plus")) { // 加
          if (val < max) {
            $(this).parent().find(".gzl-count__minus").removeClass("disabled");
            num.text(val + 1);
            if (val + 1 === max) {
              numlist.find(".gzl-count__plus").addClass("disabled");
            }
          }
        } else if ($(this).hasClass("gzl-count__minus")) { // 减
          if (val > min) {
            numlist.find(".gzl-count__plus").removeClass("disabled");
            num.text(val - 1);
            if (val - 1 === 0) {
              $(this).addClass("disabled");
            }
          }
        }
      }
    })

    // 可选服务每房每天
    var roomDayData = {perRoomPerDayList: params.perRoomPerDayList}
    $(".per-room-per-day").find(".service-select-btn").on("click",function() {
      var tpl = _.template($("#per-room-per-day").html())(roomDayData);
      $.popup({
        title: '早餐',
        content: tpl,
        btn: '确定',
        price: 88900,
        count: 8,
        btnEvent: function() {

        }
      });
      // 每房每天计算TODO
    })

    // 可选服务每房
    var roomData = {perRoomList: params.perRoomList}
    $(".per-room").find(".service-select-btn").on("click",function() {
      var tpl = _.template($("#per-room").html())(roomData);
      // console.log(tpl);
      $.popup({
        title: '迪士尼门票',
        content: tpl,
        btn: '确定',
        price: 88900,
        count: 8,
        btnEvent: function() {

        }
      });
      // 每房计算TODO
    })

    // 服务组
    var serviceGroupData = {serviceGroupList: params.serviceGroupList}
    $(".service-group").find(".service-select-btn").on("click",function() {
      var tpl = _.template($("#service-group").html())(serviceGroupData);
      // console.log(tpl);
      $.popup({
        title: '房间主题装饰名称可能很长很长很长',
        content: tpl,
        btn: '确定',
        price: 88900,
        count: 8,
        btnEvent: function() {
          
        }
      });
      // 服务组计算TODO
    })

    // 可选服务页确定按钮
    $(".confirm-btn").on("click", function() {
      params.confirmFn();
    })
  }
})($);